#!/bin/bash

#-------------------------------------------------------------------------------
# Installation return codes are:
#
# 0 - no error
# 10 - usage error
# 20 - invalid/non-existent directory on HMC
# 30 - install file not found
# 40 - RPM test verify install error
# 50 - RPM install error
# 60 - 'tar' command failure
#
# Except for call rpm command to query, all calls to rpm must go
# go through dorpm, eraserpm etc..
#
# *The actual value returned will be 1x, 2x, etc, where x will be either 0 or 4
# which is based on any RPM un-install errors.. or, '0' if successful completion
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Initialize script return code, log file name
#-------------------------------------------------------------------------------
exitRC=0
LogFile=/tmp/HmcInstall.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
exit_cleanup() {
    # keep the log file, but ensure a different user can overwrite it next time
    chmod 666 $LogFile

    if [ $1 -ne 0 ]; then
        exit $1$exitRC
    else
        exit 0
    fi
}

#-------------------------------------------------------------------------------
# RPM  install
#-------------------------------------------------------------------------------
dorpm() {
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        # Strip version then leading directory name
        f=`echo ${x%%-[0-9]*}`
        r=`echo ${f##*/}`
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$r" = "$i" ]; then
                return
            fi
        done
    fi
     
    # Log the test install output. The format for the "normal" RPM install
    # processing inside this script is 'rpm -i <file spec> --force --nodeps'
    echo "=========================================================" >> $LogFile
    echo "***** Executing rpm -vv $* *****"                          >> $LogFile
    echo "=========================================================" >> $LogFile
    rpm -vv $* >> $LogFile 2>&1
    RC=$?
    return $RC
}

#-------------------------------------------------------------------------------
# RPM erase rpm
#-------------------------------------------------------------------------------
eraserpm() {
    # If rpm is part of no update list then do not do anything with it
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`; do
            if [ "$x" = "$i" ]; then
                return
            fi
        done
    fi
    echo "=========================================================" >> $LogFile
    echo "***** Executing rpm -vv $* *****"                          >> $LogFile
    echo "=========================================================" >> $LogFile
    rpm -vv $* >> $LogFile 2>&1
    if [ $? -ne 0 ]; then
        exitRC=4
        echo "error removing rpm fileset named $2" >> $LogFile
    fi
}

#-------------------------------------------------------------------------------
# RPM test install
#-------------------------------------------------------------------------------
testrpm() {
    rpm --test  $1 $2 $3 $4
    return $?
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" = "" ]; then
    echo "Please specify directory containing installable packages"
    echo "usage: installImages  <directory>"
    exit_cleanup 1
fi

# Check if directory exists
if [ ! -d $image ]; then
    echo "The directory $patchdir doesn't exist"
    echo "Please specify directory containing the installable packages."
    exit_cleanup 2
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]; then
    rm -f /tmp/saved_files.tar
    cat /opt/hsc/data/config/NO_UPDATE_FILES | xargs tar -cvf /tmp/saved_files.tar
fi

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

if [ ! -d /opt/IBMJava ]; then
    ln -sf /opt/IBMJava2-142 /opt/IBMJava
fi

v1=`hsc version | grep Version | cut -f3 -d" "`
v2=`hsc version | grep Release | cut -f3 -d" "`
vers=`echo $v1.$v2 | cut -f1-2 -d "."`

if [ "$vers" != "4.1" -a "$vers" != "4.2" ]; then 
   echo "Incompatible Update. Updates not installed"
   exit_cleanup 3
fi

# Save old log file
if [ -f $LogFile ]; then
    mv -f $LogFile ${LogFile}.old
fi

echo "=========================================================" >> $LogFile
disk=`cat $image/.signature | cut -d' ' -f 4`
if [ $disk = "0" ]; then
    Update="true"
    echo "************   Performing Update operation   ************" >> $LogFile
else
    Update="false"
    echo "************   Performing Install operation  ************" >> $LogFile
fi
echo "=========================================================" >> $LogFile

# Installing RMC and CSM
echo "--- Installing RSCT packages...."
rpm -q src 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/src-*rpm --force --nodeps
else
    dorpm -ivh $image/rmc/src-*rpm --force --nodeps
fi

rpm -q rsct.core.utils 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/rsct.core.utils-*rpm --force --nodeps
else
    dorpm -ivh $image/rmc/rsct.core.utils-*rpm --force --nodeps
fi
rpm -q rsct.core 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/rsct.core-*rpm --force --nodeps
else
    dorpm -ivh $image/rmc/rsct.core-*rpm --force --nodeps
fi
rpm -q rsct.basic 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/rsct.basic-*rpm --force --nodeps
else
    dorpm -ivh $image/rmc/rsct.basic-*rpm --force --nodeps
fi
echo "--- Installing CSM packages...."
rpm -q csm.core 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/csm.core-*rpm --force --nodeps
else
    dorpm -ivh $image/rmc/csm.core-*rpm --force --nodeps
fi
rpm -q csm_hmc.server 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/csm_hmc.server-*rpm --force --nodeps
else
    dorpm -ivh $image/rmc/csm_hmc.server-*rpm --force --nodeps
fi
rpm -q csm.server.hsc 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/csm.server.hsc-*rpm --force --nodeps
else
    dorpm -ivh $image/rmc/csm.server.hsc-*rpm --force --nodeps
fi

if [ -f $image/.VERSION ]; then
    if [ -f $image/.dev ]; then
        cat $image/.VERSION >> /opt/hsc/data/version
    fi
fi
 
echo "==========================================================" >> $LogFile
echo "*********************** End of Log ***********************" >> $LogFile
echo "==========================================================" >> $LogFile
exit_cleanup 0
